/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package fr.monVehicule.dao.ajouter;

import fr.monVehicule.entite.Entretient;
import java.util.List;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;

/**
 *
 * @author David
 */
public class EntretienDAO extends DAO<Entretient> {

    public EntretienDAO() {
        super(Entretient.class);

    }

    public List<Entretient> findEntretientsByVehicule(Integer id) {
       
        List<Entretient> list = null;
        Criteria entretientCriteria = createCriteria();
        Criteria vehiculeCriteria = entretientCriteria.createAlias("vehicule", "vehicule");
        entretientCriteria.createAlias("vehicule.marque", "marque");
        entretientCriteria.createAlias("vehicule.modele", "modele");
        entretientCriteria.createAlias("vehicule.typeVehicule", "typeVehicule");
        vehiculeCriteria.add(Restrictions.eq("id", id));
        list = entretientCriteria.list();
        return list;

    }

    public int saveEntretient(Entretient entretien) {
        
        session.getTransaction().begin();
        Object o = session.save(entretien);
        session.getTransaction().commit();
        session.close();
        return (Integer) o;
    }

    public Entretient findEntretienById(Integer entId) {
      
        Criteria criteria = createCriteria();
        Criteria vehiculeCriteria = criteria.createAlias("vehicule", "vehicule");
        criteria.createAlias("vehicule.marque", "marque");
        criteria.createAlias("vehicule.modele", "modele");
        criteria.createAlias("vehicule.typeVehicule", "typeVehicule");
        criteria.add(Restrictions.eq("id", entId));

        return (Entretient) criteria.uniqueResult();
    }
}
